package com.ticket.sass.admin.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.dto.input.commodity.CommodityListInput;
import com.ticket.sass.admin.mapper.CommodityMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.admin.util.CinemaIdUtil;
import com.ticket.sass.common.entity.tenant.CommodityEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 卖品 服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-06
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, CommodityEntity> implements BaseService<CommodityEntity> {
    public  IPage<CommodityEntity> iPage(CommodityListInput input) {

        
        // 创建 Page 对象
        Page<CommodityEntity> page = new Page<>(input.getPageNum(), input.getPageSize());
        
        // 创建 QueryWrapper 对象
        QueryWrapper<CommodityEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(Objects.nonNull(input.getCategoryIds()) && input.getCategoryIds().length > 0, "commodity.category_id", input.getCategoryIds());
        queryWrapper.like(StrUtil.isNotEmpty(input.getName()), "commodity.name", input.getName());
        queryWrapper.eq("commodity.cinema_id", CinemaIdUtil.getCinemaIdFromRequest());
        
        if (Objects.nonNull(input.getHasAttr())) {
            queryWrapper.inSql("commodity.id", "SELECT commodity_id FROM commodity_skus WHERE has_attr =" + input.getHasAttr());
        }
        
        queryWrapper.eq(Objects.nonNull(input.getPublishStatus()), "commodity.publish_status", input.getPublishStatus());
        
        
        // 在此根据需要添加其他查询条件
        queryWrapper.orderByDesc("commodity.id");
        
        queryWrapper.isNull("commodity.deleted_at");
        
        // 调用 this.page 方法
        return this.baseMapper.queryPage(page, queryWrapper);
    }
    
    public CommodityEntity getById(Long id) {
        // 创建 QueryWrapper 对象
        QueryWrapper<CommodityEntity> queryWrapper = new QueryWrapper<CommodityEntity>().eq("commodity.id", id);
        queryWrapper.isNull("commodity.deleted_at");
        // 调用 this.page 方法
        return this.baseMapper.queryList(queryWrapper).get(0);
    }
    
    public List<CommodityEntity> queryCommoditiesByCouponId(Long couponId){
        return this.baseMapper.queryCommoditiesByCouponId(couponId);
    }
    
    
    /**
     * 根据商品ID列表查询对应的商品信息，并返回以商品ID为键的Map
     *
     * @param commodityIds 商品ID列表
     * @return 以商品ID为键的Map，值为对应的商品实体对象
     */
    @Override
    public Map<Long, CommodityEntity> mapByIds(List<Long> commodityIds) {
        if (commodityIds.isEmpty()){
            return new HashMap<>();
        }
       
        // 使用 MyBatis-Plus 的 selectBatchIds 方法根据商品ID列表查询对应的商品信息列表
        List<CommodityEntity> commodities = this.baseMapper.selectBatchIds(commodityIds);
        
        // 使用 Java 8 的流式操作将结果列表转换为以商品ID为键的Map
        Map<Long, CommodityEntity> commodityMap = commodities.stream()
            .collect(Collectors.toMap(CommodityEntity::getId, commodity -> commodity));
        
        return commodityMap;
    }
}
